#ifndef SRV_ASYNCH_MAN_H
#define SRV_ASYNCH_MAN_H
#include <map>
#include <string>
using namespace std;
class   CCriticalSection;
class   Srv_Service_Handler;
class   Asynch_Write_Queue;
class   Asynch_Read_Queue;
class   Asynch_Return_Queue;
class   Epoll_Impl;
class Srv_Asynch_Man
{
    friend class Asynch_Write_Queue;
    friend class Asynch_Read_Queue;
    friend class Asynch_Return_Queue;
    friend class Srv_Service_Handler;
    friend class Epoll_Impl;
public:
    Srv_Asynch_Man();
    virtual ~Srv_Asynch_Man(void);
public:
    int Open (const char *pAddr,int nPort);
private:
    virtual Srv_Service_Handler *make_handler (int client_socket);//工厂函数 创建连接

//epoll__Impl  call
private:
    void add_service_handler(int client_socket);		//当连接到来（内部实现）
    void delete_service_handler(int client_socket);     //当连接结束（内部实现）
    void post_socket_send(int client_socket);           //通知套接字可发送数据（内部实现）
    void post_socket_read(int client_socket);           //通知套接字可recv数据（内部实现）
    void all_delete_service_handler();					//当结束服务结束（内部实现）

//  call
private:
    void append_read_buf(int client_socket,string & send_buf);//当数据到来
    void destroy_connection(int client_socket);//异步销毁关闭
    void append_write_buf(int client_socket,const char *pBuf,int nlen);//当数据到来

//Asynch_Return_Queue call to Srv_Service_Handler
private:
    void handle_read(int client_socket,const char *pBuf,int nlen);

//Asynch_Read_Queue Asynch_Write_Queue  call to Epoll_Impl
private:
    bool handle_send_passive(int client_socket,string & send_buf);
    bool handle_send_active(int client_socket,string & send_buf);
    void handle_read_passive(int client_socket,string & read_buf);

private:
    Srv_Service_Handler * find_service_handler(int client_socket);

private:
    //std::map<int,handle_buf>        map_manage_socket;
    std::map<int,Srv_Service_Handler*>	map_socket_handler;

    CCriticalSection                *m_poCriSection_socket;//同步map_manage_socket 资源锁

    Asynch_Read_Queue               *m_pAsynch_Read_Queue;
    Asynch_Write_Queue              *m_pAsynch_write_queue;
    Asynch_Return_Queue             *m_pAsynch_Return_Queue;

    Epoll_Impl                      *m_pEpoll_Impl;
};

#endif // SRV_ASYNCH_MAN_H
